<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>bufferedviewportsizemonitor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_dom_bufferedviewportsizemonitor.js.html">bufferedviewportsizemonitor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2012 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A viewport size monitor that buffers RESIZE events until the
<a name="line17"></a> * window size has stopped changing, within a specified period of time.  For
<a name="line18"></a> * every RESIZE event dispatched, this will dispatch up to two *additional*
<a name="line19"></a> * events:
<a name="line20"></a> * - {@link #EventType.RESIZE_WIDTH} if the viewport&#39;s width has changed since
<a name="line21"></a> *   the last buffered dispatch.
<a name="line22"></a> * - {@link #EventType.RESIZE_HEIGHT} if the viewport&#39;s height has changed since
<a name="line23"></a> *   the last buffered dispatch.
<a name="line24"></a> * You likely only need to listen to one of the three events.  But if you need
<a name="line25"></a> * more, just be cautious of duplicating effort.
<a name="line26"></a> *
<a name="line27"></a> */
<a name="line28"></a>
<a name="line29"></a>goog.provide(&#39;goog.dom.BufferedViewportSizeMonitor&#39;);
<a name="line30"></a>
<a name="line31"></a>goog.require(&#39;goog.asserts&#39;);
<a name="line32"></a>goog.require(&#39;goog.async.Delay&#39;);
<a name="line33"></a>goog.require(&#39;goog.events&#39;);
<a name="line34"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line35"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line36"></a>
<a name="line37"></a>
<a name="line38"></a>
<a name="line39"></a>/**
<a name="line40"></a> * Creates a new BufferedViewportSizeMonitor.
<a name="line41"></a> * @param {!goog.dom.ViewportSizeMonitor} viewportSizeMonitor The
<a name="line42"></a> *     underlying viewport size monitor.
<a name="line43"></a> * @param {number=} opt_bufferMs The buffer time, in ms. If not specified, this
<a name="line44"></a> *     value defaults to {@link #RESIZE_EVENT_DELAY_MS_}.
<a name="line45"></a> * @constructor
<a name="line46"></a> * @extends {goog.events.EventTarget}
<a name="line47"></a> */
<a name="line48"></a>goog.dom.BufferedViewportSizeMonitor = function(
<a name="line49"></a>    viewportSizeMonitor, opt_bufferMs) {
<a name="line50"></a>  goog.base(this);
<a name="line51"></a>
<a name="line52"></a>  /**
<a name="line53"></a>   * The underlying viewport size monitor.
<a name="line54"></a>   * @type {goog.dom.ViewportSizeMonitor}
<a name="line55"></a>   * @private
<a name="line56"></a>   */
<a name="line57"></a>  this.viewportSizeMonitor_ = viewportSizeMonitor;
<a name="line58"></a>
<a name="line59"></a>  /**
<a name="line60"></a>   * The current size of the viewport.
<a name="line61"></a>   * @type {goog.math.Size}
<a name="line62"></a>   * @private
<a name="line63"></a>   */
<a name="line64"></a>  this.currentSize_ = this.viewportSizeMonitor_.getSize();
<a name="line65"></a>
<a name="line66"></a>  /**
<a name="line67"></a>   * The resize buffer time in ms.
<a name="line68"></a>   * @type {number}
<a name="line69"></a>   * @private
<a name="line70"></a>   */
<a name="line71"></a>  this.resizeBufferMs_ = opt_bufferMs ||
<a name="line72"></a>      goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_;
<a name="line73"></a>
<a name="line74"></a>  /**
<a name="line75"></a>   * Listener key for the viewport size monitor.
<a name="line76"></a>   * @type {goog.events.Key}
<a name="line77"></a>   * @private
<a name="line78"></a>   */
<a name="line79"></a>  this.listenerKey_ = goog.events.listen(
<a name="line80"></a>      viewportSizeMonitor,
<a name="line81"></a>      goog.events.EventType.RESIZE,
<a name="line82"></a>      this.handleResize_,
<a name="line83"></a>      false,
<a name="line84"></a>      this);
<a name="line85"></a>};
<a name="line86"></a>goog.inherits(goog.dom.BufferedViewportSizeMonitor, goog.events.EventTarget);
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Additional events to dispatch.
<a name="line91"></a> * @enum {string}
<a name="line92"></a> */
<a name="line93"></a>goog.dom.BufferedViewportSizeMonitor.EventType = {
<a name="line94"></a>  RESIZE_HEIGHT: goog.events.getUniqueId(&#39;resizeheight&#39;),
<a name="line95"></a>  RESIZE_WIDTH: goog.events.getUniqueId(&#39;resizewidth&#39;)
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Delay for the resize event.
<a name="line101"></a> * @type {goog.async.Delay}
<a name="line102"></a> * @private
<a name="line103"></a> */
<a name="line104"></a>goog.dom.BufferedViewportSizeMonitor.prototype.resizeDelay_;
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Default number of milliseconds to wait after a resize event to relayout the
<a name="line109"></a> * page.
<a name="line110"></a> * @type {number}
<a name="line111"></a> * @const
<a name="line112"></a> * @private
<a name="line113"></a> */
<a name="line114"></a>goog.dom.BufferedViewportSizeMonitor.RESIZE_EVENT_DELAY_MS_ = 100;
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/** @override */
<a name="line118"></a>goog.dom.BufferedViewportSizeMonitor.prototype.disposeInternal =
<a name="line119"></a>    function() {
<a name="line120"></a>  goog.events.unlistenByKey(this.listenerKey_);
<a name="line121"></a>  goog.base(this, &#39;disposeInternal&#39;);
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * Handles resize events on the underlying ViewportMonitor.
<a name="line127"></a> * @private
<a name="line128"></a> */
<a name="line129"></a>goog.dom.BufferedViewportSizeMonitor.prototype.handleResize_ =
<a name="line130"></a>    function() {
<a name="line131"></a>  // Lazily create when needed.
<a name="line132"></a>  if (!this.resizeDelay_) {
<a name="line133"></a>    this.resizeDelay_ = new goog.async.Delay(
<a name="line134"></a>        this.onWindowResize_,
<a name="line135"></a>        this.resizeBufferMs_,
<a name="line136"></a>        this);
<a name="line137"></a>    this.registerDisposable(this.resizeDelay_);
<a name="line138"></a>  }
<a name="line139"></a>  this.resizeDelay_.start();
<a name="line140"></a>};
<a name="line141"></a>
<a name="line142"></a>
<a name="line143"></a>/**
<a name="line144"></a> * Window resize callback that determines whether to reflow the view contents.
<a name="line145"></a> * @private
<a name="line146"></a> */
<a name="line147"></a>goog.dom.BufferedViewportSizeMonitor.prototype.onWindowResize_ =
<a name="line148"></a>    function() {
<a name="line149"></a>  if (this.viewportSizeMonitor_.isDisposed()) {
<a name="line150"></a>    return;
<a name="line151"></a>  }
<a name="line152"></a>
<a name="line153"></a>  var previousSize = this.currentSize_;
<a name="line154"></a>  var currentSize = this.viewportSizeMonitor_.getSize();
<a name="line155"></a>
<a name="line156"></a>  goog.asserts.assert(currentSize,
<a name="line157"></a>      &#39;Viewport size should be set at this point&#39;);
<a name="line158"></a>
<a name="line159"></a>  this.currentSize_ = currentSize;
<a name="line160"></a>
<a name="line161"></a>  if (previousSize) {
<a name="line162"></a>
<a name="line163"></a>    var resized = false;
<a name="line164"></a>
<a name="line165"></a>    // Width has changed
<a name="line166"></a>    if (previousSize.width != currentSize.width) {
<a name="line167"></a>      this.dispatchEvent(
<a name="line168"></a>          goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_WIDTH);
<a name="line169"></a>      resized = true;
<a name="line170"></a>    }
<a name="line171"></a>
<a name="line172"></a>    // Height has changed
<a name="line173"></a>    if (previousSize.height != currentSize.height) {
<a name="line174"></a>      this.dispatchEvent(
<a name="line175"></a>          goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_HEIGHT);
<a name="line176"></a>      resized = true;
<a name="line177"></a>    }
<a name="line178"></a>
<a name="line179"></a>    // If either has changed, this is a resize event.
<a name="line180"></a>    if (resized) {
<a name="line181"></a>      this.dispatchEvent(goog.events.EventType.RESIZE);
<a name="line182"></a>    }
<a name="line183"></a>
<a name="line184"></a>  } else {
<a name="line185"></a>    // If we didn&#39;t have a previous size, we consider all events to have
<a name="line186"></a>    // changed.
<a name="line187"></a>    this.dispatchEvent(
<a name="line188"></a>        goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_HEIGHT);
<a name="line189"></a>    this.dispatchEvent(
<a name="line190"></a>        goog.dom.BufferedViewportSizeMonitor.EventType.RESIZE_WIDTH);
<a name="line191"></a>    this.dispatchEvent(goog.events.EventType.RESIZE);
<a name="line192"></a>  }
<a name="line193"></a>};
<a name="line194"></a>
<a name="line195"></a>
<a name="line196"></a>/**
<a name="line197"></a> * Returns the current size of the viewport.
<a name="line198"></a> * @return {goog.math.Size?} The current viewport size.
<a name="line199"></a> */
<a name="line200"></a>goog.dom.BufferedViewportSizeMonitor.prototype.getSize = function() {
<a name="line201"></a>  return this.currentSize_ ? this.currentSize_.clone() : null;
<a name="line202"></a>};
</pre>


</body>
</html>
